package com.bank.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bank.entity.*;

@Service
public class BaseDao {
	
	Logger logger  = Logger.getLogger(BaseDao.class);

	@Autowired
	private SessionFactory sessionFactory;
	@SuppressWarnings("unchecked") 
	public String getWorkDate(){
		Session session = sessionFactory.getCurrentSession();
		String sql = "from System_Workdate ";
		Query query = session.createQuery(sql);
		List<System_Workdate> list =  query.list();
		System_Workdate  sysWokedate = null;
		if(list.size()==0)
			return null;
		logger.debug(list.size());
		for( System_Workdate sysWokedate1:list){
			sysWokedate = sysWokedate1;
		}
		return sysWokedate.getCurr_wordate();
	}
	@SuppressWarnings("unchecked") 
	public Subject getSubjectByNumer(String subject_number){
		String sql = "from Subject where subject_number='"+subject_number+"'";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(sql);
		List<Subject> list = query.list();
		Subject subject = null;
		if(list.size()==0)
		{
			return null;
		}
		logger.debug("list.size():"+list.size());
		for(Subject subjectFor:list){
			subject = subjectFor;
		}
		return subject;
	}
	public String  getAccountSerno(){
		String sql = "from  SystemParam where param_key='ACCT_SERNO'";
		GetGeneralBean<SystemParam>  generalBean = new GetGeneralBean<SystemParam>();
		SystemParam param = generalBean.getGenerlBean(sql);
		String serno = param.getParam_value();
		Integer  sernoInt = Integer.parseInt(serno)+1;
		String sernoPlus = String.valueOf(sernoInt);
		param.setParam_value(sernoPlus);
		generalBean.update(param);
		return serno;
	}
		
	//查询基础实体类
	@SuppressWarnings("unchecked") 
	class GetGeneralBean<T>{
		public T getGenerlBean(String sql){
			Session session = sessionFactory.getCurrentSession();
			Query query = session.createQuery(sql);
			List<T> list = query.list();
			T t = null;
			if(list.size()==0)
			{
				return t;
			}
			logger.debug("list.size():"+list.size());
			for(T t1:list){
				t = t1;
			}
			return t;
	 
		}
		public void update(T t){
			Session session = sessionFactory.getCurrentSession();
			session.update(t);
			session.flush();
		}

	}
}
